<?php

class Ext_Publish_Attachment_Add
{
    protected $_articleId;
    protected $_userId;
    
    protected $_messager;
    const ARTICLE_RECORD_NOT_FOUND   = 'articleRecordNotFound';
    protected $_messageTemplates = array(
        self::ARTICLE_RECORD_NOT_FOUND   => "Article record not found.",
    );
    
    public function __construct($articleId)
    {
        $userId = Ext_Auth_Front::getInstance()->getIdentity();
        
        $this->_articleId = $articleId;
        $this->_userId    = $userId;
        $this->_messager  = new Ext_Messager($this->_messageTemplates);
    }
    
    public function add(Ext_Publish_Attachment_Add_Interface $addAdapter)
    {
        $_result = array(
            'code'      => Ext_Publish_Attachment_Add_Result::FAILURE,
            'datas'     => array(),
            'messages'  => array(),
        );
        
        $_articleRow = App_Model_DbTable_Article::getInstance()->getRowById($this->_articleId);
        if(!$_articleRow){
            $_result['messages'][] = $this->_messager->addMessage(self::ARTICLE_RECORD_NOT_FOUND);
            return new Ext_Publish_Attachment_Add_Result($_result['code'], $_result['datas'], $_result['messages']);
        }
        
        $_addResulter = $addAdapter->add($this->_articleId);
        if(!$_addResulter->isValid())
        {
            $_result['messages'] = $_addResulter->getMessages();
            return new Ext_Publish_Attachment_Add_Result($_result['code'], $_result['datas'], $_result['messages']);
        }
        
        $_result['code']  = Ext_Publish_Attachment_Add_Result::SUCCESS;
        $_result['datas'] = $_addResulter->getDatas();
        return new Ext_Publish_Attachment_Add_Result($_result['code'], $_result['datas'], $_result['messages']);
        
    }
}
